package com.ym.jiujiangeyou;

import com.ym.jiujiangeyou.app.AppManager;
import com.ym.jiujiangeyou.ui.DialogView;
import com.ym.jiujiangeyou.util.Constants;
import com.ym.jiujiangeyou.util.HttpUtil;

import android.app.Activity;
import android.app.Dialog;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author 叶铭
 * @email yeming_1001@163.com
 * @version V1.0
 */
public class MoreFeedBack extends Activity {

	TextView title_text;
	EditText send_message;
	RelativeLayout rel_commit;
	String message = null;
	String name = null;
	Toast toast;
	Dialog dialog;
	boolean isFlag = false;
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case Constants.OPTION_FAIL:
				dialog.dismiss();
				toast = DialogView.popToast(MoreFeedBack.this,
						R.string.option_fail);
				toast.show();
				break;
			case Constants.OPTION_SUCCESS:
				send_message.setText(R.string.send_message_hint_1);
				send_message.setSelection(send_message.length());
				isFlag = true;
				dialog.dismiss();
				toast = DialogView.popToast(MoreFeedBack.this,
						R.string.send_message_success);
				toast.show();
				break;
			default:
				break;
			}

		}
	};

	public MoreFeedBack() {
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 添加Activity到堆栈
		AppManager.getAppManager().addActivity(this);
		super.setContentView(R.layout.more_feed_back);

		title_text = (TextView) findViewById(R.id.title_text);
		title_text.setText(R.string.more_feed_back);
		send_message = (EditText) findViewById(R.id.send_message);
		rel_commit = (RelativeLayout) findViewById(R.id.rel_commit);
		dialog = DialogView.popDialog(this, R.string.send_message_ing);

		/*
		 * send_message.setOnFocusChangeListener(new OnFocusChangeListener() {
		 * 
		 * @Override public void onFocusChange(View v, boolean hasFocus) { //
		 * TODO Auto-generated method stub if(hasFocus && isFlag){
		 * send_message.setText(R.string.send_message_hint) ; isFlag = false ; }
		 * } }) ;
		 */

		rel_commit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				message = send_message.getText().toString();
				if (message != null && send_message.length() > 0) {
					dialog.show();
					new Thread(new Runnable() {
						Message mg = new Message();

						@Override
						public void run() {
							// TODO Auto-generated method stub
							String result = HttpUtil.SendMessage(name, message);
							if (result.equals(Constants.CONNECTION_TIMEOUT)
									|| result.equals("FAIL")) {
								mg.what = Constants.OPTION_FAIL;
							} else {
								mg.what = Constants.OPTION_SUCCESS;

							}
							handler.sendMessage(mg);
						}
					}).start();
				} else {
					toast = DialogView.popToast(MoreFeedBack.this,
							R.string.send_message_null);
					toast.show();
				}
			}
		});
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		AppManager.getAppManager().finishActivity(this);

	}
}
